<?php
// 录入评论
namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

use App\Models\Topic;
use App\Models\Reply;

class ProcessTopicReply implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $data;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * 1. 增加话题评论数
     * 2. 评论内容数据入库
     * @param Topic $topic
     * @param Reply $reply
     */
    public function handle(Topic $topic, Reply $reply)
    {

        $topic->where(['id' => $this->data['topic_id']])->increment('reply_count', 1, ['last_reply_user_id' => $this->data['user_id']]);

        $this->data['created_at'] = $this->data['updated_at'] = date('Y-m-d H:i:s');
        $reply->insert($this->data);
    }
}
